package edu.towson.cis.cosc442.project3.vendingmachine2;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class VendingMachineTest {

	VendingMachine vend;

	//SetUp method
	@Before
	public void setUp() throws Exception {
		vend= new VendingMachine();
		vend.addItem(new VendingMachineItem("pie",1.0),"A");
	}

	//Testing to see  if an item can be added to slot B
	@Test
	public void testAddItemB(){
		vend.addItem(new VendingMachineItem("cake",1.0),"B");
		

	}
	//Testing to see  if an item can be added to slot C
	@Test
	public void testAddItemC(){
		vend.addItem(new VendingMachineItem("chips",.75),"C");
		

	}
	//Testing to see  if an item can be added to slot D
	@Test
	public void testAddItemD(){
		vend.addItem(new VendingMachineItem("chocolate",.50),"D");
		

	}
	/*
	 * Testing to see  if failure condition is met 
	 * if attempting to add to a no existing slot
	 */
	@Test (expected=VendingMachineException.class)
	public void testAddItemSlotFail(){
		vend.addItem(new VendingMachineItem("Crowbar",1.0),"E");
		

	}
	/*
	 * Testing to see  if failure condition is met 
	 * if attempting to add to a filled slot slot
	 */
	@Test(expected=VendingMachineException.class)
	public void testAddItemOverlapFail(){
		
		vend.addItem(new VendingMachineItem("cake",1.0),"A");

	}
	
	//Testing to see  if an item is successfully removed
	@Test
	public void testRemoveItem(){
		String name="pie";
		double price=1.0;
		VendingMachineItem result = vend.removeItem("A");
		assertNotNull(result);
		assertEquals(price,result.getPrice(),.01);
		assertSame(name,result.getName());
		
	}
	
	/*
	 * Testing to see  if failure condition is met
	 * when trying to delete from an empty slot
	 */
	@Test(expected=VendingMachineException.class)
	public void testRemoveItemFail(){

		vend.removeItem("B");
	}

	//tests to see if money can be inserted
	@Test
	public void testInsertMoney(){
		double balance=2.00;
		vend.insertMoney(2.00);
		assertEquals(balance,vend.getBalance(),.01);
	}
	
	//tests to see if insertion fails if value is less than 0
	@Test(expected=VendingMachineException.class)
	public void testInsertMoneyFail(){
		vend.insertMoney(-1);
	}
	
	//tests the getter for Balance
	@Test
	public void testgetBalance(){
		vend.insertMoney(2.0);
		assertEquals(2.0,vend.getBalance(), .1);
	}
	
	
	//tests to see if item can be purchased successfully
	@Test
	public void testMakePurchase(){
		vend.insertMoney(2.0);
		assertTrue(vend.makePurchase("A"));
	}
	
	//tests to see if empty item slot is not purchased successfully
	@Test
	public void testMakePurchaseFail(){
		vend.insertMoney(2.0);
		assertFalse(vend.makePurchase("B"));
	}		
	
	//tests to see if item is not purchased because of no money successfully
	@Test
	public void testMakePurchaseMoneyFail(){
		//vend.insertMoney(2.0);
		assertFalse(vend.makePurchase("A"));
	}
	//tests to see if empty item slot is not purchased because of no money successfully
		@Test
		public void testMakePurchaseMoneyItemFail(){
			//vend.insertMoney(2.0);
			assertFalse(vend.makePurchase("B"));
		}
	
	
	
	
	//tests to see if the correct amount of change is returned
	@Test
	public void testreturnChange(){
		vend.insertMoney(2.0);
		assertEquals(2.0,vend.returnChange(), .1);
	}
	
	//Teardown method
	@After
	public void tearDown() throws Exception {
		vend=null;
	}

}
